package petstore.web.servlet.item;

//import org.jetbrains.annotations.NotNull;

import petstore.domain.Comment;
import petstore.domain.Item;
import petstore.domain.Product;
import petstore.persistence.CommentDaoImpl;
import petstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
//import javax.servlet.*;
//import javax.servlet.jsp.PageContext;

//import static petstore.web.servlet.login.LoginServlet.username;

public class ItemServlet extends HttpServlet {
    private String ITEM_PAGE="/WEB-INF/jsp/ItemPage/itemPage.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productId = req.getParameter("productId");

        HttpSession session = req.getSession();
        String username = (String) session.getAttribute("username");
        if(productId == null){
           productId = (String) session.getAttribute("productId");
        }
//        System.out.println("测试====="+productId);
        CatalogService catalogService = new CatalogService();//实例化一个CatalogService对象
        Product product = catalogService.getProduct(productId);
        Item item = catalogService.getItem(productId);
        req.setCharacterEncoding("UTF-8");
        String com = req.getParameter("COM"); //获取得到的评论
//        System.out.println(item);
//        System.out.println(com);
        CommentDaoImpl commentDao = new CommentDaoImpl(); //获取评论
        if(com != null)
        {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = simpleDateFormat.format(System.currentTimeMillis());
            commentDao.insertCommentByUsername(username,com,time,item.getName());
        }
        item = catalogService.getItem(productId);

        List<Comment> commentList = commentDao.getCommentByUsernameAndItemname(item.getName());
        System.out.println("item :" + item.getName());
        if(commentList.isEmpty())
            System.out.println("commentList为空");
   //     System.out.println(ProductId);
     //   System.out.println(product.getName());
       //System.out.println(item.getName());
        //向jsp传递
//        String com = req.getParameter("comment");
//        catalogService.addComment(com,item.getName());
        if(username!=null)
        {
            session.setAttribute("username",username);
        }
        System.out.println("dwqasdasdsa::" + item.getName());
        session.setAttribute("commentList",commentList);
        session.setAttribute("productId",productId);
        session.setAttribute("product",product);
        session.setAttribute("item",item);
//        System.out.println("评论+"+product);
//        System.out.println("传给下个界面的值"+item);
        req.getRequestDispatcher(ITEM_PAGE).forward(req,resp);
    }
}
